package com.codeczx.taglayoutdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by codeczx on 2017/5/19.
 */

public class MyTagsLayout extends ViewGroup {

    private int childHorizontalSpace;
    private int childVerticalSpace;
    private static final String TAG = "MyTagsLayout";

    public MyTagsLayout(Context context) {
        this(context, null);
    }

    public MyTagsLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTagsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagsLayout);
        if (typedArray != null) {
            childHorizontalSpace = typedArray.getDimensionPixelSize(R.styleable.TagsLayout_tagHorizontalSpace, 0);
            childVerticalSpace = typedArray.getDimensionPixelSize(R.styleable.TagsLayout_tagVerticalSpace, 0);
            typedArray.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        // 如果mode为WRAP_CONTENT，要记录MyTagsLayout的大小
        int width = 0;
        int height = 0;

        int lineWidth = 0;
        int lineHeight = 0;
        int left = getPaddingLeft();
        int top = getPaddingTop();

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = lp.leftMargin + lp.rightMargin + childHorizontalSpace + child.getMeasuredWidth();
            int childHeight = lp.topMargin + lp.bottomMargin + childVerticalSpace + child.getMeasuredHeight();

            if (lineWidth + childWidth > widthSpecSize - getPaddingLeft() - getPaddingRight()) {
                // 取最大的值，因为WRAP_CONTENT需要最大值
                width = Math.max(lineWidth, childWidth);
                lineWidth = childWidth;
                height += lineHeight;
                lineHeight = childHeight;
                child.setTag(new Location(left, top + height, left + childWidth - childHorizontalSpace, top + height + child.getMeasuredHeight()));
            } else {
                child.setTag(new Location(left + lineWidth, top + height, left + lineWidth + childWidth-childHorizontalSpace, top + height + child.getMeasuredHeight()));
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            Log.i(TAG, "onMeasure: i:"+i);
            Log.i(TAG, "onMeasure: width"+width);
            Log.i(TAG, "onMeasure: height"+height);
        }

        width = Math.max(lineWidth, width) + getPaddingLeft() + getPaddingRight();
        height += lineHeight;
        heightSpecSize += getPaddingTop() + getPaddingBottom();
        height += getPaddingTop() + getPaddingBottom();
        setMeasuredDimension(widthSpecMode == MeasureSpec.EXACTLY ? widthSpecSize : width, heightSpecMode == MeasureSpec.EXACTLY ? heightSpecSize : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if(child.getVisibility()==View.GONE){
                continue;
            }
            Location location = (Location) child.getTag();
            child.layout(location.left,location.top,location.right,location.bottom);
            Log.i(TAG, "onLayout: "+location.toString());
        }
    }

    private class Location {

        private int left;
        private int top;
        private int right;
        private int bottom;

        public Location(int left, int top, int right, int bottom) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }

        @Override
        public String toString() {
            return "Location{" +
                    "left=" + left +
                    ", top=" + top +
                    ", right=" + right +
                    ", bottom=" + bottom +
                    '}';
        }
    }

    @Override
    protected TagsLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new TagsLayout.LayoutParams(p);
    }

    @Override
    protected TagsLayout.LayoutParams generateDefaultLayoutParams() {
        return new TagsLayout.LayoutParams(TagsLayout.LayoutParams.MATCH_PARENT, TagsLayout.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public TagsLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new TagsLayout.LayoutParams(getContext(), attrs);
    }


    // 继承自margin，支持子视图android:layout_margin属性
    public static class LayoutParams extends MarginLayoutParams {


        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }


        public LayoutParams(int width, int height) {
            super(width, height);
        }


        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }


        public LayoutParams(ViewGroup.MarginLayoutParams source) {
            super(source);
        }
    }
}
